Configuration - web.xmlTo configure WebWork's component management, the following lines must be added in the appropriate places to web.xml: <listener> <listener-class>com.opensymphony.webwork.lifecycle.SessionLifecycleListener</listener-class> </listener> <listener> <listener-class>com.opensymphony.webwork.lifecycle.ApplicationLifecycleListener</listener-class> </listener> These settings allow WebWork to manage components across the application, session and request scopes. Note that even if one or more of the scopes are not required by your application, all three scopes need to be specified in web.xml for WebWork's component management to function correctly. Configuration - xwork.xmlThe ComponentInterceptor class is used to apply the IoC pattern to XWork actions (ie, to supply components to actions). The ComponentInterceptor should be declared in the <interceptors> block of xwork.xml as follows: <interceptor name="component" class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/> You should ensure that any actions that are to be supplied with components have this interceptor applied. (See OS:XWork Interceptors for information on how to apply interceptors to actions.) Note too, that the ComponentInterceptor is applied as part of the webwork defaultStack. Thus, if you are applying the defaultStack to the action, you would include the ComponentInterceptor. Configuration - components.xmlThe components.xml file is used to specify the components that are to be available. The components specified here are loaded into XWork's ComponentManager and are then made available to any actions that are an instance of the specified enabler. The components.xml file must be placed in the root of the classpath (ie, in the WEB-INF/classes directory). <components> <component> <scope>session</scope> <class>com.opensymphony.webwork.example.counter.Counter</class> <enabler>com.opensymphony.webwork.example.counter.CounterAware</enabler> </component> </components> Each component must have the following three attributes:
Note that while components are allowed to have dependencies on other components they must not depend on another component that is of a narrower scope. So for example, a session component cannot depend on a component that is only of request scope. |